<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动方块</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
</head>

<body>
    <div id="app">

        <div v-bind:style="a"></div>
        <input type="text" v-on:keyup.up.down.left.right='test'>

    </div>
    <script>
        let app = new Vue({
            el: "#app",
            data: {
                a: {
                    width: '10px',
                    height: '10px',
                    backgtound: 'red',
                    position: "relative",
                    left: '0px',
                    top: '0px',
                }
            },
            methods: {
                test: function (e) {
                    let x = e.keyCode
                    if (x == 37 || x == 39) {
                        let c = this.a.left.replace('px', '')
                        c = parseInt(c)   //强制转换

                        let l = x == 37 ? c - 10 : c + 10
                        this.a.left = 1 + 'px'
                    } else {
                        let c = this.a.top.replace('px', '')
                        c = parseInt(c)
                        let l = x == 38 ? c - 10 : c + 10
                        this.a.top = 1 + 'px'

                    }
                }

            }
        })


    </script>



</body>

</html>